﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IFTRUE.DataCollection.BLL;
using IFTRUE.DataCollection.BLL.Custom;

namespace IFTRUE.DataCollection.Win
{
    public partial class App : Form
    {
        private System.Threading.CancellationTokenSource _cts;
        private int iUpdate;
        private int iAdd;
        private long startCode;

        public App()
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (btnOK.Text == "启 动")
            {
                StartTr();
            }
            else
            {
                EndTr();
            }
        }

        /// <summary>
        /// 启动
        /// </summary>
        private void StartTr()
        {
            SetReStart();//初始化数值             
            timer1.Interval = Convert.ToInt32(txtTime.Text);
            timer1.Enabled = true;
            timer1_Tick(null, null);
            btnOK.Text = "终 止";
        }

        /// <summary>
        /// 终 止
        /// </summary>
        private void EndTr()
        {
            if (_cts != null)
                _cts.Cancel();
            if (timer1.Enabled)
                timer1.Enabled = false;
            btnOK.Text = "启 动";
            SetReStart();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _cts = new System.Threading.CancellationTokenSource();
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(JD), _cts.Token); // _cts.Token
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void SetReStart()
        {
            startCode = Convert.ToInt64(txtStart.Text);
            progressBar1.Value = 0;
            progressBar1.Maximum = Convert.ToInt32(txtInterval.Text);
        }

        private void JD(object o)
        {
            Model.commodity model = null;
            BLL.commodity bll = new BLL.commodity();
            BLL.commodityRecord crBll = new commodityRecord();
            bool bl = false;
            iUpdate = 0;
            iAdd = 0;
            for (long i = startCode; i < startCode + Convert.ToInt32(txtInterval.Text); i++)
            {
                //判断Timer是否终止,终止则跳出当前循环 并记录已获取的记录数
                if (_cts.IsCancellationRequested)
                {
                    break;
                }
                model = WebPageCollectionBLL.JSCommodity(i, System.Text.Encoding.GetEncoding("gb2312"));
                if (model != null)
                {
                    if (bll.ExistsEx(model.code))
                    {
                        bl = bll.UpdateEx(model);
                        iUpdate++;
                    }
                    else
                    {
                        model.guid = Guid.NewGuid().ToString();
                        bl = bll.Add(model);
                        iAdd++;
                    }
                    //添加价格记录
                    Model.commodityRecord crModel = new Model.commodityRecord();
                    crModel.guid = Guid.NewGuid().ToString();
                    crModel.code = model.code;
                    crModel.createDate = DateTime.Now;
                    crModel.mall = "京东商城";
                    crModel.price = Convert.ToDecimal(model.price);
                    crBll.Add(crModel);
                }
                if (progressBar1.Value < progressBar1.Maximum)
                {
                    progressBar1.Value = progressBar1.Value + 1;
                }
            }
            long iRow = long.Parse(iAdd.ToString()) + long.Parse(iUpdate.ToString());
            lbMsg.Items.Add("新增商品信息：" + iAdd.ToString() + "条，更新商品信息：" + iUpdate.ToString() + "条;商品编号【" + startCode.ToString() + "--" + (startCode + iRow - 1).ToString() + "】");
            txtStart.Text = (startCode + iRow).ToString();//(startCode + Convert.ToInt32(txtInterval.Text)).ToString();

            if (progressBar1.Value == progressBar1.Maximum)
            {
                EndTr();
            }
            else
                progressBar1.Value = 0;
        }
    }
}
